package com.example.fszq.volleydemo1;

import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.example.fszq.volleydemo1.utils.Parsehtml;
import com.example.fszq.volleydemo1.utils.Requesttoserver;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by fszq on 2015/12/16.
 */
public class GetCatagory {
    private JSONArray catListJsonArray;
    private List<vv> catSource = new ArrayList<vv>();

    private Requesttoserver Rserver = new Requesttoserver();
    private RequestQueue quence ;
    private Parsehtml Parse = new Parsehtml();

    public GetCatagory() {

    }


    public JSONArray getCat1() {

        try {
            catListJsonArray = new JSONArray();

            String[] s = {"1","【话说佛山】","c2000/tb/foshan.gif","城市共同睇","33"};
            vv src = new vv();
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "130";
            s[1] = "【广州新语】";
            s[2] = "c2000/tb/guangzhou.gif";
            s[3] = "城市共同ti";
            s[4] = "100";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "176";s[1] = "【春秋笔谈】";s[2] = "c2000/tb/ls.gif";s[3] = "我有我主张";s[4] = "10";;
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "176";s[1] = "【春秋笔谈】";s[2] = "c2000/tb/ls.gif";s[3] = "我有我主张";s[4] = "1";;
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "27";s[1] = "【传媒春秋】";s[2] = "c2000/tb/cm.gif";s[3] = "我有我主张";;s[4] = "13";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "6";s[1] = "【商机楼市】";s[2] = "c2000/tb/lou.gif";s[3] = "我有我主张";s[4] = "110";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "8";s[1] = "【火热体坛】";s[2] = "c2000/tb/sport.gif";s[3] = "我有我主张";s[4] = "22";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "64";s[1] = "【爱心在线】";s[2] = "c2000/tb/ls.gif";s[3] = "我有我主张";s[4] = "21";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "176";s[1] = "【春秋笔谈】";s[2] = "c2000/tb/ls.gif";s[3] = "我有我主张";s[4] = "13";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "176";s[1] = "【春秋笔谈】";s[2] = "c2000/tb/ls.gif";s[3] = "我有我主张";s[4] = "14";
            src.setvv(s);
            catSource.add(src);

            src = new vv();
            s[0] = "176";s[1] = "【春秋笔谈】";s[2] = "c2000/tb/ls.gif";s[3] = "我有我主张";s[4] = "7";
            src.setvv(s);
            catSource.add(src);

            //执行排序操作
            Collections.sort(catSource, new Comparator<vv>() {
                @Override
                public int compare(vv vv, vv t1) {
                    int flag = vv.getIdx() - t1.getIdx();
                    //Log.d("sortidx",flag+"");
                    return flag;
                }
            });

            for(vv t:catSource) {
                JSONObject catSubJson = new JSONObject();

                catSubJson.put("cid",t.getCid());
                catSubJson.put("cname",t.getCname());
                catSubJson.put("clogo","http://www.c2000.cn/" + t.getClogo());
                catListJsonArray.put(catSubJson);
            }





            //Log.d("catjson:",catListJsonArray.toString());
        } catch (Exception e) {
            e.printStackTrace();
            //Log.d("catajsonerror:",e.getMessage());
        }


        return catListJsonArray;
    }

    public JSONArray getCat() {
        try{
            catListJsonArray = new JSONArray();

            String url = "http://www.c2000.cn/index.asp";

            StringRequest stringRequest = new StringRequest(url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            //把返回的内容写入进行解析并列出
                            //return catListJsonArray;

                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("TAG", error.getMessage(), error);
                }


            }){
                @Override
                protected Response<String> parseNetworkResponse(NetworkResponse response) {
                    String str = null;
                    try {
                        str = new String(response.data, "gb2312");//这里写死不好哦
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return Response.success(str, HttpHeaderParser.parseCacheHeaders(response));
                }
            };

            quence.add(stringRequest);
            return catListJsonArray;
        } catch (Exception e) {
            //Log.e("TAG", e.getMessage(), e);
            e.printStackTrace();
            return null;
        }
    }

    private class vv {
        private String cid,cname,clogo,ctype;
        private int idx = 0;
        private void setvv(String[] init) {
            cid = init[0];
            cname = init[1];
            clogo = init[2];
            ctype = init[3];
            idx = Integer.parseInt(init[4]);
            Log.d("listidx",idx+"");
        }

        public String getCid() {
            return cid;
        }

        public String getCname() {
            return cname;
        }

        public String getClogo() {
            return clogo;
        }

        public String getCtype() {
            return ctype;
        }

        public int getIdx(){ return idx;}
    }

}

